Lesson Objective
- Learn how to display information on the screen and understand what a variable is.
- Understand some basic Java syntax.
KS3, GCSE, A-Level Computing Resources
Every line of code that runs in Java must be inside a class. In the example above, we named the class Main. A class should always start with an uppercase first letter.
public class Main { public static void main(String args[]){ System.out.println("Hello World, welcome to your first Java lesson."); } }
Hello World, welcome to your first Java lesson.
The main() method is used in every Java program. Any code inside the main() method will be executed. Don't worry about the keywords before and after main. You will get to know them bit by bit while reading this tutorial. For now, just remember that every Java program has a class name which must match the filename, and that every program must contain the main() method.
To display information to the user, you must use the "System.out.println" method.
public class Hello_World_1 { public static void main(String args[]){ System.out.println("Hello World, welcome to your first Java lesson."); } }
Hello World, welcome to your first Java lesson. I'm on the next line.
The "System.out.println" method is used to create new lines in the display.
public class Hello_World_1 { public static void main(String args[]){ System.out.println("Hello World, welcome to your first Java lesson."); System.out.println("I'm on the next line."); } }
Hello World, welcome to your first Java lesson. I'm on the next line.
The "System.out.print" method allows you to stay on the same line in the display.
public class Hello_World_1 { public static void main(String args[]){ System.out.print("Hello World."); System.out.print("I'm not... on the next line."); } }
Hello World. I'm not... on the next line.
Variables are storage locations in your code that store data. They are assigned "tags" and are referred to throughout a program.
public class Hello_World_1 { public static void main(String args[]){ System.out.println("Hello World."); String cat; cat = "Hello World again..."; System.out.println(cat); } }
Hello World Hello World again...
To get the user to enter data into your program, you would use the "Scanner" class. You will need to import this function first.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ System.out.println("Enter three words: "); Scanner scan_ob = new Scanner(System.in); cat = "Hello World again..."; System.out.println(scan_ob.nextLine()); } }
Enter three words: apple bird chair apple bird chair
.nextLine() is used to display everything that was entered into the keyboard up to the point you press Enter.
.next() is used to display single works in the input, upto the space. I think. I can't remember, but it's not important now.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ System.out.println("Enter three words: "); Scanner scan_ob = new Scanner(System.in); cat = "Hello World again..."; System.out.println(scan_ob.next()); } }
Enter three words: apple bird chair apple
The scanner object can be saved into a variable once the user has used the keyboard to enter some data.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ Scanner scan_ob = new Scanner(System.in); double fnum, snum, answer; System.out.println("Enter your first number: "); fnum = scan_ob.nextDouble(); System.out.println("Enter your second number: "); snum = scan_ob.nextDouble(); answer = fnum + snum; System.out.println(answer); } } // Variables of a similar data type can be set on the same line. // Method that specifies the input data type.
Enter your first number: 12.3 Enter your first number: 3.2 15.5
Here is a table of all the different Scanner methods that can be used for data input.
Method | Description |
---|---|
.nextBoolean() | Reads a boolean value from the user |
.nextByte() | Reads a byte value from the user |
.nextDouble() | Reads a double value from the user |
.nextFloat() | Reads a float value from the user |
.nextInt() | Reads a int value from the user |
.nextLine() | Reads a String value from the user |
.nextLong() | Reads a long value from the user |
.nextShort() | Reads a short value from the user |
Data types are divided into two groups:
The main difference between primitive and non-primitive data types are:
A primitive data type specifies the size and type of variable values, and it has no additional methods.
There are eight primitive data types in Java.
Method | Description | Description |
---|---|---|
byte | 1 bytes | Stores whole numbers from -128 to 127 |
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
boolean | 1 bit | Stores true or false values |
char | 2 bytes | Stores a single character/letter or ASCII values |
A constant is like a variable. It stores data, but the data does not change.
import java.util.Scanner; import java.lang.Math; class apple { public static void main(String args[]){ System.out.println("Hello World"); Scanner scan_ob = new Scanner(System.in); final double pi = 3.1314159628; double radius, area; System.out.println("Enter Radius: "); radius = scan_ob.nextDouble(); area = pi * (Math.pow(radius,2)); System.out.println(area); } }
Enter Radius: 20 1252.56638512
The final keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override).
The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...).
The final keyword is called a "modifier".